/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.pneuservis.soap.sei.impl;

import cz.muni.fi.pa165.pneuservis.soap.dto.ServiceDTO;
import cz.muni.fi.pa165.pneuservis.soap.dto.Transfer;
import cz.muni.fi.pa165.pneuservis.soap.sei.ServiceSEI;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jws.WebService;
import pneuservis.backend.services.ServicePneuServiceLocal;
import pneuservis.backend.to.ServicePneuTO;

@WebService(endpointInterface="cz.muni.fi.pa165.pneuservis.soap.sei.ServiceSEI", serviceName="ServiceService")
@Stateless
public class ServiceService implements ServiceSEI {

    @EJB(lookup="java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/ServicePneuService")
    private ServicePneuServiceLocal servicePneuService;
    
    @Override
    public void createService(ServiceDTO service) {
        servicePneuService.createService(Transfer.toServicePneuTO(service));
    }

    @Override
    public void updateService(ServiceDTO service) {
        servicePneuService.updateService(Transfer.toServicePneuTO(service));
    }

    @Override
    public void removeService(ServiceDTO service) {
        servicePneuService.removeService(Transfer.toServicePneuTO(service));
    }

    @Override
    public ServiceDTO getService(Long id) {
        return Transfer.toServiceDTO(servicePneuService.getService(id));
    }

    @Override
    public ServiceDTO[] getAllServices() {
        List<ServicePneuTO> allServices = servicePneuService.getAllServices();
        return asServiceArray(allServices);
    }
    
    @Override
    public ServiceDTO[] findServiceWithName(String name){
        return asServiceArray(servicePneuService.findServiceWithName(name));
    }

    private ServiceDTO[] asServiceArray(List<ServicePneuTO> allServices) {
        List<ServiceDTO> result = new ArrayList<>();
        for(ServicePneuTO s : allServices){
            result.add(Transfer.toServiceDTO(s));
        }
        ServiceDTO[] a = new ServiceDTO[0];
        return result.toArray(a);
    }
    
}
